package com.kedll.library.widget.pullDownMenu;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.kedll.library.listener.SimpleAnimationListener;
import com.kedll.library.util.ViewUtil;

/**
 * 下拉菜单
 *
 * @author QiQi
 * @date 16/6/21
 */
public class PullDownMenuView extends FrameLayout implements OnDisplayClickListener {
    View subView;
    View attachView;
    Animation inAnim, outAnim, alphaAnim;
    private boolean isExpand = false;
    private OnDisplayClickListener onDisplayClickListener;
    private PullDownMenuViewAdapter adapter;

    public PullDownMenuView(Context context) {
        this(context, null);
    }

    public PullDownMenuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public PullDownMenuView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setBackgroundColor(Color.parseColor("#80000000"));
        setVisibility(GONE);
        setClickable(true);

        inAnim = AnimationUtils.loadAnimation(context, com.kedll.library.R.anim.top_in_600);
        outAnim = AnimationUtils.loadAnimation(context, com.kedll.library.R.anim.top_out_600);
        alphaAnim = AnimationUtils.loadAnimation(context, android.R.anim.fade_out);
        alphaAnim.setAnimationListener(new SimpleAnimationListener() {
            @Override
            public void onAnimationEnd(Animation animation) {
                super.onAnimationEnd(animation);
                ViewUtil.setGone(PullDownMenuView.this, true);
            }
        });

        setOnClickListener(view -> PullDownMenuView.this.switchDisplay(attachView));
    }

    public void setOnDisplayClickListener(OnDisplayClickListener onItemClickListener) {
        this.onDisplayClickListener = onItemClickListener;
    }

    /**
     * 绑定适配器
     *
     * @param adapter
     */
    public void setAdapter(PullDownMenuViewAdapter adapter) {
        if (attachView == null) {
            throw new IllegalArgumentException("请先调用attachControlView方法");
        }
        if (this.adapter == adapter) {
            return;
        }

        removeAllViews();
        if (adapter != null) {
            //获取需要添加的View
            subView = adapter.getView(this);
            LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            addView(subView, params);
            adapter.setAttachView(attachView);
            this.adapter = adapter;
        }
    }

    /**
     * 绑定控制显示或隐藏的控件
     *
     * @param attachView
     */
    public void attachControlView(View attachView) {
        if (this.attachView == attachView) {
            return;
        }
        this.attachView = attachView;
//        this.attachView.setOnClickListener(PullDownMenuView.this::switchDisplay);
    }

    /**
     * v
     * 切换显示
     */
    public void switchDisplay(View view) {
        if (!isExpand) {
            onExpand(view);
        } else {
            onCollapse(view);
        }
    }

    //显示选择界面
    @Override
    public void onExpand(View view) {
        if (subView != null) {
            subView.clearAnimation();
            subView.startAnimation(inAnim);
        }

        clearAnimation();
        ViewUtil.setGone(this, false);
        isExpand = true;
        if (onDisplayClickListener != null) {
            onDisplayClickListener.onExpand(view);
        }
    }

    //隐藏选择界面
    @Override
    public void onCollapse(View view) {
        if (subView != null) {
            subView.clearAnimation();
            subView.startAnimation(outAnim);
        }

        clearAnimation();
        startAnimation(alphaAnim);
        isExpand = false;

        if (onDisplayClickListener != null) {
            onDisplayClickListener.onCollapse(view);
        }
    }

    public View getAttachView() {
        return attachView;
    }

    public boolean isExpand() {
        return isExpand;
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.attachView != null) {
            this.attachView.setOnClickListener(null);
            this.attachView = null;
        }
        removeAllViews();
        subView = null;
    }

    public interface PullDownMenuViewAdapter {
        void setAttachView(View attachView);

        View getView(OnDisplayClickListener itemClickListener);
    }
}
